package com.guigu.handler;


import cn.hutool.core.util.ObjectUtil;
import com.guigu.annotation.SubjectInfoType;
import com.guigu.domain.enums.SubjectInfoEnum;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;


import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @description: 题目添加处理工厂
 * @author: JZC
 * @create: 2024-06-06 23:48
 **/
@Component
public class SubjectInfoFactory implements ApplicationContextAware {


    private Map<String, SubjectInfoHandler> subjectInfoHandlers = new ConcurrentHashMap<>();

    public SubjectInfoHandler getHandlers(Integer subjectInfoType) {
        for (Map.Entry<String, SubjectInfoHandler> entry : subjectInfoHandlers.entrySet()) {
            SubjectInfoType subjectType = entry.getValue().getClass().getAnnotation(SubjectInfoType.class);
            if (ObjectUtil.isNotEmpty(subjectType) && ObjectUtil.equal(subjectInfoType, subjectType.type().getCode())) {
                return entry.getValue();
            }
        }
        return null;
    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        subjectInfoHandlers = applicationContext.getBeansOfType(SubjectInfoHandler.class);
    }
}
